﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if NETFX_CORE
using Windows.UI.Xaml.Data;
#else
using System.Windows.Data;
#endif

namespace ArchiSimple.Windows
{
    public class EnumToBooleanConverter : IValueConverter
    {
        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return this.Convert(value, targetType, parameter, culture.Name);
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return this.ConvertBack(value, targetType, parameter, culture.Name);
        }

        public virtual object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
            {
                return false;
            }

            return String.Equals(value.ToString(), parameter);
        }

        public virtual object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            var valueBoolean = (bool)value;

            if (valueBoolean)
            {
                return Enum.Parse(targetType, (string)parameter);
            }
            else
            {
                return null;
            }
        }
    }
}
